﻿using Microsoft.Phone.Controls;
using Microsoft.Phone.Net.NetworkInformation;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace Indt.Components
{
    public class TapToRetry
    {
        private ProgressIndicator progressIndicator;
        //private DependencyObject _element;
        private string _url;

        public TapToRetry(/*DependencyObject element,*/ string url, string message) 
        {
            //_element = element;
            _url = url;

            progressIndicator = new ProgressIndicator();
            progressIndicator.Text = message;
            progressIndicator.IsIndeterminate = true;
        }

        public void InternetCheck(Action success = null, Action error = null)
        {
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                progressIndicator.IsVisible = true;

               // SystemTray.SetIsVisible(_element, true);
              //  SystemTray.SetProgressIndicator(_element, progressIndicator);

                WebClient webClient = new WebClient();

                DispatcherTimer webClientTimer = new DispatcherTimer();
                webClientTimer.Tick += new EventHandler((sender, args) =>
                {
                    webClient.CancelAsync();
                    ((DispatcherTimer)sender).Stop();
                });
                webClientTimer.Interval = new TimeSpan(0, 0, 10);
                webClientTimer.Start();

                DownloadStringCompletedEventHandler handler = null;

                handler = (s, e) =>
                {
                    webClient.DownloadStringCompleted -= handler;

                    progressIndicator.IsVisible = false;

                    if (!e.Cancelled)
                    {
                        if (e.Error == null)
                        {
                            webClientTimer.Stop();
                            if (success != null)
                            {
                                success();
                            }
                        }
                        else
                        {
                            webClientTimer.Stop();
                            if (error != null)
                            {
                                error();
                            }
                        }
                    }
                    else
                    {
                        if (error != null)
                        {
                            error();
                        }
                    }
                };

                webClient.DownloadStringCompleted += handler;
                webClient.DownloadStringAsync(new Uri(_url));
            }
            else 
            {
                if (error != null)
                {
                    error();
                }            
            }
        }
    }
}
